nwnfandomcom-20200213-history
Respawn chest
This respawn chest script allows chests (placeables) to be regenerated. (The inspiration was placing treasure chests within an area, but any placeable can be used.) The module builder marks the locations where the chests appear. When this script is run, instances of a single placeable blueprint are created at each of those locations, and any objects remaining from an earlier run are destroyed. This script was written as the onEnter event handler for a trigger, but can be used for any event, or even executed by other means. The main requirement is that it is executed by an object within an area or by the area itself (but not by the module). Processing is done per area, so execution by an object within the area is equivalent to execution by the area. There is a delay (600 seconds, or 10 minutes) built into the script to prevent it from firing too often. To use this script, the builder must place objects, usually waypoints, to mark where the placeables will appear (paying attention to both position and facing). These objects must have the same tag, which is specified as WP_CHEST in the below script. The builder must also prepare (or choose) a blueprint from which the chests will be created. This blueprint must include any lock or trap that is desired; half of the created objects will become unlocked, and an independent half will become untrapped, but it is up to the builder to specify the lock and trap for the remainder. The ResRef of this blueprint can replace plc_chest1 in the below script, or it can be specified as affecting only one area by setting a local string named vtCHEST on the area. // ** configuration ** const float DELAY_REBUILD = 600.0; // Delay in seconds until the chests respawn. const string TAG_MARKER = "WP_CHEST"; // Tag of the objects marking where chests appear. const string RESREF_CHEST = "plc_chest1"; // Default ResRef of the chest to create; may be overridden by the area. // If some of the chests should be locked, the ResRef must be set to "locked". // If some of the chests should be trapped, the ResRef must include a trap. const string LV_RESREF_CHEST = "vtCHEST"; // Name of the local string (on an area) that overrides the default chest ResRef. // ** internal constants ** const string LV_CHEST = "vtCHEST__Mine"; const string LV_TIMER = "vtCHEST__Suppressed"; void main() { object oArea = GetArea(OBJECT_SELF); // Do not run too often. if ( GetLocalInt(oArea, LV_TIMER) ) return; SetLocalInt(oArea, LV_TIMER, TRUE); AssignCommand(oArea, DelayCommand(DELAY_REBUILD, SetLocalInt(oArea, LV_TIMER, FALSE))); // What chest to use? string sChestResRef = GetLocalString(oArea, LV_RESREF_CHEST); if ( sChestResRef "" ) sChestResRef = RESREF_CHEST; // Loop through the markers in this area. int nNth = 1; object oAnchor = GetFirstObjectInArea(oArea); // In case OBJECT_SELF is the area. object oMarker = GetNearestObjectByTag(TAG_MARKER, oAnchor, nNth); while ( OBJECT_INVALID != oMarker ) { // Destroy any existing chest. DestroyObject(GetLocalObject(oMarker, LV_CHEST)); // Create a chest. object oChest = CreateObject(OBJECT_TYPE_PLACEABLE, sChestResRef, GetLocation(oMarker)); SetLocalObject(oMarker, LV_CHEST, oChest); if ( Random(2) ) SetLocked(oChest, FALSE); // 50% unlocked if ( Random(2) ) SetTrapDisabled(oChest); // 50% not trapped // Update the loop. oMarker = GetNearestObjectByTag(TAG_MARKER, oAnchor, ++nNth); } } category:Script_directory:Items and Placeables